package com.awifi.service.impl;

import com.awifi.dao.AccountDao;
import com.awifi.entity.Account;
import com.awifi.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@EnableTransactionManagement
@EnableAspectJAutoProxy
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    public void saveAccount() {
        System.out.println("保存成功！");
    }
    public void updateAccount(int i) {
        System.out.println("更新成功！"+i);
    }

    public int deleteAccount() {
        System.out.println("删除成功！");
        return 0;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
    public void transform(String source, String target, float money) {
        Account account1 = accountDao.findAccountByName(source);
        Account account2 = accountDao.findAccountByName(target);

        account1.setMoney(account1.getMoney()-money);
        accountDao.update(account1);

        int i = 1/0;
        account2.setMoney(account2.getMoney()+money);
        accountDao.update(account2);
    }
}
